package com.allenkerr.other;

import org.junit.Test;

import java.util.Map;
import java.util.TreeMap;

/**
 * Created by 小柯 on 17/7/10.
 */
public class OtherTest02 {

    @Test
    public void test1() {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(4, "def");
        treeMap.put(1, "abc");
        treeMap.put(11, "abc");
        treeMap.put(3, "cde");
        treeMap.put(33, "cde");
        treeMap.put(2, "bcd");
        treeMap.put(21, "bcd");

        for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
            System.out.println(entry.getKey() + "\t\t" + entry.getValue());
        }
    }

    @Test
    public void test2() {
        TreeMap<Double, String> treeMap = new TreeMap<>();
        treeMap.put(4.0, "def");
        treeMap.put(1.0, "abc");
        treeMap.put(11.0, "abc");
        treeMap.put(3.0, "cde");
        treeMap.put(33.0, "cde");
        treeMap.put(2.0, "bcd");
        treeMap.put(21.0, "bcd");

        for (Map.Entry<Double, String> entry : treeMap.entrySet()) {
            System.out.println(entry.getKey() + "\t\t" + entry.getValue());
        }
    }

    @Test
    public void test3() {
        int number = 16;
        System.out.println(number >> 4);//除以16
    }

    @Test
    public void test4() {
        //r + g<<4 + b<<8，也即 r + g*16 + b*16*16
        //假设 RGB 值为(14, 68, 221), 16等份之后，得到(0, 4, 13)，它对应直方图索引值(index)为
        //index = 0 + 4*16 + 13 * 16 * 16 = 3392
        //index = 0 + 4<<4 + 13<<8 = ?

        int red = 14, green = 68, blue = 221;
        System.out.println(red + ", " + green + ", " + blue);   //14, 68, 221

        System.out.println((red >> 4) + (green >> 4 << 4) + (blue >> 4 << 8));//3392

        red = red >> 4;
        green = green >> 4;
        blue = blue >> 4;
        System.out.println(red + ", " + green + ", " + blue);   //0, 4, 13

        int index = red + green * 16 + blue * 16 * 16;
        System.out.println("index = " + index);                 //index = 3392

        index = red + (green << 4) + (blue << 8);
        System.out.println("index = " + index);                 //index = 3392
    }

    @Test
    public void test5() {
        final int NUMBER_SHIFT = 4; //移位
        int red = 14, green = 68, blue = 221;
        int index = (red >> NUMBER_SHIFT) + (green >> NUMBER_SHIFT << NUMBER_SHIFT)
                + (blue >> NUMBER_SHIFT << (NUMBER_SHIFT << 1));
        System.out.println("index = " + index);     //index = 3392
    }

    @Test
    public void test6() {
        final int NUMBER_SHIFT = 4; //移位
        int red = 14, green = 68, blue = 221;
        //等分
        red = (red >> NUMBER_SHIFT) & 0xFF;
        green = (green >> NUMBER_SHIFT) & 0xFF;
        blue = (blue >> NUMBER_SHIFT) & 0xFF;
        //求下标
//        int index = red + (green << NUMBER_SHIFT) + (blue << (NUMBER_SHIFT << 1));
        int index = red + (green << NUMBER_SHIFT) + (blue << NUMBER_SHIFT << NUMBER_SHIFT);
        System.out.println("index = " + index);     //index = 3392
    }

    @Test
    public void test7() {
        int number = 2;
        System.out.println(number << 2);        //乘以4
        System.out.println(number << 2 << 2);   //乘以4，再乘以4
    }

    @Test
    public void test8() {
        final int NUMBER_SHIFT = 4; //移位
        int red = 255, green = 255, blue = 255;
        //等分
        red = (red >> NUMBER_SHIFT) & 0xFF;
        green = (green >> NUMBER_SHIFT) & 0xFF;
        blue = (blue >> NUMBER_SHIFT) & 0xFF;

        System.out.println("red = " + red);         //15
        System.out.println("green = " + green);     //15
        System.out.println("blue = " + blue);       //15

        //求下标
        int index = red + (green << NUMBER_SHIFT) + (blue << NUMBER_SHIFT << NUMBER_SHIFT);
        System.out.println("index = " + index);     //index = 4095
    }


}
